//    Copyright (C) Kherty.  All rights reserved.
using System.Windows;
using System.Windows.Input;
using OpenLS.Spreadsheet.UI.Internals;
using OpenLS.Spreadsheet.UI.Internals.Resources;

namespace OpenLS.Spreadsheet.UI
{
    class GridBorders
    {
        internal static void EnableCommands(UIElement g)
        {
            AddBorderCommandBinding(g,  BorderCommands.ClearBorders,
                OnClearBorder);
            AddBorderCommandBinding(g,  BorderCommands.SetLeftBorder,
                OnSetLeftBorder);
            AddBorderCommandBinding(g,  BorderCommands.SetRightBorder,
                OnSetRightBorder);
            AddBorderCommandBinding(g,  BorderCommands.SetTopBorder,
                OnSetTopBorder);
            AddBorderCommandBinding(g,  BorderCommands.SetBottomBorder,
                OnSetBottomBorder);
            AddBorderCommandBinding(g,  BorderCommands.SetAllBorders,
                OnSetAllBorders);
            AddBorderCommandBinding(g,  BorderCommands.SetOutsideBorders,
                OnSetOutsideBorders);
            AddBorderCommandBinding(g,  BorderCommands.SetTopAndBottomBorders,
                OnSetTopAndBottomBorders);
        }
        static void AddBorderCommandBinding (UIElement g, RoutedCommand command, GridCommandHelper.MultiAction<Worksheet, object> action)
        {
            GridCommandHelper.AddWorksheetCommandBinding(g, command,action, WorksheetHelper.DefaultProtectionFunction);
        }
        static void OnClearBorder(Worksheet worksheet, object parameter)
        {
            worksheet.Selection.ClearBorders();
        }

        private static Worksheet GetWorksheet(object sender)
        {
            return WorksheetHelper.GetWorksheet(sender);
        }
        static void OnSetLeftBorder(Worksheet worksheet, object parameter)
        {
            OnSetSomeBorder(worksheet, parameter, BorderType.LeftEdge);

        }
        static void OnSetTopAndBottomBorders(Worksheet worksheet, object parameter)
        {
          
            using (worksheet.CreateUndoScope(BorderUnitName))
            {
                worksheet.Selection.SetBorder(Border.Create(BorderStyle.Medium), BorderType.TopEdge);
                worksheet.Selection.SetBorder(Border.Create(BorderStyle.Medium), BorderType.BottomEdge);
               
            } 
        }
        static void OnSetRightBorder(Worksheet worksheet, object parameter)
        {
            OnSetSomeBorder(worksheet, parameter, BorderType.RightEdge);
        }
        static void OnSetTopBorder(Worksheet worksheet, object parameter)
        {
            OnSetSomeBorder(worksheet, parameter, BorderType.TopEdge);
        }

        private static void OnSetSomeBorder(Worksheet worksheet, object parameter, BorderType borderType)
        {
            worksheet.Selection.SetBorder(Border.Create(GetBorderStyle(parameter)), borderType);
        }

        static void OnSetBottomBorder(Worksheet worksheet, object parameter)
        {
            OnSetSomeBorder(worksheet, parameter, BorderType.BottomEdge);
        }
        static readonly string BorderUnitName = UIStringTable.Borders;
        static void OnSetAllBorders(Worksheet worksheet, object parameter)
        {
            OnSetSomeBorder(worksheet, parameter, BorderType.All);

        }

        private static BorderStyle GetBorderStyle(object parameter)
        {
            BorderStyle borderStyle = BorderStyle.Thin;
            if (parameter != null)
                borderStyle = (BorderStyle)parameter;
            return borderStyle;
        }
        static void OnSetOutsideBorders(Worksheet worksheet, object parameter)
        {
            var range =  worksheet.Selection;
            BorderStyle borderStyle = GetBorderStyle(parameter);
            range.SetBorder(Border.Create(borderStyle), BorderType.OutsideEdge);
        }
    }
}
